package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.CoreServiceImpl;
import com.jinzhi.jzapi.certificate.pojo.ConsultationsVo;
import com.jinzhi.jzweb.dao.ConsultationsDao;
import com.jinzhi.jzweb.domain.ConsultationsDO;
import com.jinzhi.jzweb.service.ConsultationsService;
import com.jinzhi.sys.dao.RoleConsultationsDao;
import com.jinzhi.sys.domain.RoleConsultationsDO;

import lombok.AllArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * <pre>
 * 资讯表
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Service
@AllArgsConstructor
public class ConsultationsServiceImpl extends CoreServiceImpl<ConsultationsDao, ConsultationsDO> implements ConsultationsService {

	private final RoleConsultationsDao roleConsultationsMapper;
	
    @Override
    public Page<ConsultationsVo> selectListPage(int current, int number,String firstColumn, String columnName) {

        // 新建分页
        Page<ConsultationsVo> page = new Page<ConsultationsVo>(current, number);

        // 根据情况，返回分页结果
        if(StringUtils.isNotBlank(columnName)){
            return page.setRecords(this.baseMapper.dyGetUserList2(page,firstColumn,columnName));
        }else{
            if(StringUtils.isNotBlank(firstColumn)){
                return page.setRecords(this.baseMapper.dyGetUserList3(page,firstColumn));
            }else{
                return page.setRecords(this.baseMapper.dyGetUserList1(page));
            }

        }
    }
    
    @Override
    public Page<ConsultationsVo> selectListByUserIdPage(int current, int number,String firstColumn, String columnName,Long userId) {

        // 新建分页
        Page<ConsultationsVo> page = new Page<ConsultationsVo>(current, number);

        // 根据情况，返回分页结果
        if(StringUtils.isNotBlank(columnName)){
            return page.setRecords(this.baseMapper.dyGetUserList7(page,firstColumn,columnName,userId));
        }else{
            if(StringUtils.isNotBlank(firstColumn)){
                return page.setRecords(this.baseMapper.dyGetUserList3(page,firstColumn));
            }else{
                return page.setRecords(this.baseMapper.dyGetUserList1(page));
            }

        }
    }
    
    @Transactional
    @Override
    public boolean insert(ConsultationsDO consultations) {

        int count = baseMapper.insert(consultations);
        List<Long> roles = consultations.getRoleIds();
        Long consultationsId = consultations.getId();
        roleConsultationsMapper.removeByConsultationsId(consultationsId);
        List<RoleConsultationsDO> list = new ArrayList<>();
        if (Objects.nonNull(roles)) {
            for (Long roleId : roles) {
                RoleConsultationsDO rc = new RoleConsultationsDO();
                rc.setConsultationsId(consultationsId);
                rc.setRoleId(roleId);
                list.add(rc);
            }
        }
        
        if (list.size() > 0) {
            int i = roleConsultationsMapper.batchSave(list);
        }
        
        return retBool(count);
    }
    
    @Transactional
    @Override
    public boolean update(ConsultationsDO consultations) {

        int count = baseMapper.updateById(consultations);
        List<Long> roles = consultations.getRoleIds();
        Long consultationsId = consultations.getId();
        roleConsultationsMapper.removeByConsultationsId(consultationsId);
        List<RoleConsultationsDO> list = new ArrayList<>();
        if (Objects.nonNull(roles)) {
            for (Long roleId : roles) {
                RoleConsultationsDO rc = new RoleConsultationsDO();
                rc.setConsultationsId(consultationsId);
                rc.setRoleId(roleId);
                list.add(rc);
            }
        }
        
        if (list.size() > 0) {
            int i = roleConsultationsMapper.batchSave(list);
        }
        
        return retBool(count);
    }

    @Override
    public Page<ConsultationsVo> selectThumbnailListPage(int current, int number,String firstColumn, String columnName) {

        // 新建分页
        Page<ConsultationsVo> page = new Page<ConsultationsVo>(current, number);

        // 根据情况，返回分页结果
        if(StringUtils.isNotBlank(columnName)){
            return page.setRecords(this.baseMapper.dyGetUserList5(page,firstColumn,columnName));
        }else{
            if(StringUtils.isNotBlank(firstColumn)){
                return page.setRecords(this.baseMapper.dyGetUserList6(page,firstColumn));
            }else{
                return page.setRecords(this.baseMapper.dyGetUserList4(page));
            }

        }
    }

    @Override
    public Page<ConsultationsDO> findList(int current, int number, String secondColumn) {

        // 新建分页
        Page<ConsultationsDO> page = new Page<ConsultationsDO>(current, number);

        return page.setRecords(this.baseMapper.findList(page , secondColumn));
    }
}
